//
//  AllowancesViewController.m
//  ActSistant
//
//  Created by Hieu Nguyen Trung on 3/7/13.
//  Copyright (c) 2013 Savvycom. All rights reserved.
//

#import "AllowancesViewController.h"
#import "ActPayrollCalculatorFeeViewController.h"
#import "AllowancesCell.h"

@interface AllowancesViewController ()

@end

@implementation AllowancesViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initializeViews];
}

-(void) initializeViews{
    [self.navigationView.btnBack addTarget:self action:@selector(btnBackClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationView.btnRight1 addTarget:self action:@selector(btnSettingClicked) forControlEvents:UIControlEventTouchUpInside];
    
    [self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(33, 0)];
    [self.searchBar setBackgroundImage:[UIImage imageNamed:@"SearchBarBackground.png"]];
    [self.searchBar setImage:[UIImage imageNamed:@"SearchIcon.png"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    [self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"SearchFieldBackground.png"] forState:UIControlStateNormal];
    [self.searchBar setDelegate:self];
    
    for (UIView *view in [self.searchBar subviews]){
        if ([view isKindOfClass:[UITextField class]]){
            [((UITextField *)view) setLeftViewMode:UITextFieldViewModeNever];
            [((UITextField *)view) setClearButtonMode:UITextFieldViewModeWhileEditing];
            
            self.lblSearchPlaceholder = [[[UILabel alloc] initWithFrame:CGRectMake(45, 0, 150, 44)] autorelease];
            [self.lblSearchPlaceholder setTextColor:[UIColor whiteColor]];
            [self.lblSearchPlaceholder setBackgroundColor:[UIColor clearColor]];
            [self.lblSearchPlaceholder setFont:UIFontSystem(14)];
            [self.lblSearchPlaceholder setText:@"Filter Results"];
            [self.lblSearchPlaceholder setHidden:false];
            [self.searchBar addSubview:self.lblSearchPlaceholder];
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_navigationView release];
    [_searchBar release];
    [_tableView release];
    [_lblSearchPlaceholder release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setNavigationView:nil];
    [self setSearchBar:nil];
    [self setTableView:nil];
    [super viewDidUnload];
}

#pragma mark - Action Handle
-(void) btnBackClicked{
    [self.navigationController popViewControllerAnimated:true];
}

-(void) btnSettingClicked{
    ActSettingViewController *settingVC = [[[ActSettingViewController alloc] init] autorelease];
    [self.navigationController pushViewController:settingVC animated:true];
}

#pragma mark - UISearchBarDelegate Methods Implementation
-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
}

-(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if ([searchBar.text isEqualToString:@""]){
        [self.lblSearchPlaceholder setHidden:false];
    }
    else{
        [self.lblSearchPlaceholder setHidden:true];
    }
}

#pragma mark - UITableView Methods Implementation
-(int) numberOfSectionsInTableView:(UITableView *)tableView{
    return [TYPES count];
}

-(int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return [SELECT_ALLOWANCES_LABELS_STRING_ARRAY count];
            break;
        case 1:
            return [STUDIO_CONDITIONS_LABELS_STRING_ARRAY count];
            break;
        case 2:
            return [MAKEUP_LABELS_STRING_ARRAY count];
            break;
        default:
            return [VEHICLES_LABELS_STRING_ARRAY count];
            break;
    }
}

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
    [label setText:TYPES[section]];
    [label setFont:UIFontSystem(20)];
    [label setTextColor:[UIColor darkGrayColor]];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setBackgroundColor:[UIColor clearColor]];
    
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[Util imageWithColor:[UIColor colorWithRed:224/255.0 green:224/255.0 blue:224/255.0 alpha:1]]];
    [imgView setFrame:CGRectMake(0, 43, 320, 1)];
    
    [view addSubview:imgView];
    
    [view addSubview:label];
    return view;
}

-(void) scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat sectionHeaderHeight = 44;
    if (scrollView.contentOffset.y <= sectionHeaderHeight&& scrollView.contentOffset.y >= 0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }
    else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell;
    
    NSArray *objectsInXIB = [[NSBundle mainBundle] loadNibNamed:@"PayrollAgencyCell" owner:nil options:nil];
    cell = objectsInXIB[1];
    
    
    switch (indexPath.section) {
        case 0:
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
            [((AllowancesCell *) cell).lblPrice setText:@""];
            [((AllowancesCell *) cell).lblTitle setText:SELECT_ALLOWANCES_LABELS_STRING_ARRAY[indexPath.row]];
            UIView *cellBackgroundView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
            [cellBackgroundView setBackgroundColor:[UIColor whiteColor]];
            [((AllowancesCell *) cell) setSelectedTitleColor:[UIColor lightGrayColor]];
            [((AllowancesCell *) cell) setSelectedBackgroundView:cellBackgroundView];
            break;
        case 1:
            [((AllowancesCell *) cell).lblTitle setText:STUDIO_CONDITIONS_LABELS_STRING_ARRAY[indexPath.row]];
            [((AllowancesCell *) cell).lblPrice setText:STUDIO_CONDITIONS_PRICE_ARRAY[indexPath.row]];
            break;
        case 2:
            [((AllowancesCell *) cell).lblTitle setText:MAKEUP_LABELS_STRING_ARRAY[indexPath.row]];
            [((AllowancesCell *) cell).lblPrice setText:MAKEUP_PRICE_ARRAY[indexPath.row]];
            break;
        case 3:
            [((AllowancesCell *) cell).lblTitle setText:VEHICLES_LABELS_STRING_ARRAY[indexPath.row]];
            [((AllowancesCell *) cell).lblPrice setText:VEHICLES_PRICE_ARRAY[indexPath.row]];
            break;
        default:
            break;
    }

    return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 1 && indexPath.section == 0){
        ActPayrollCalculatorFeeViewController *vc = [[[ActPayrollCalculatorFeeViewController alloc] initWithType:ActPayrollCaculatorTypeCustomsAllowance] autorelease];
        [self.navigationController pushViewController:vc animated:true];
    }
}
@end
